﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

/// <summary>
/// Handler 的摘要说明
/// </summary>
public abstract class Handler
{
	public Handler(IHttpContextAccessor context)
	{
        this.Request = context.HttpContext.Request;
        this.Response = context.HttpContext.Response;
	}

    public abstract Task Process();

    protected async Task WriteJson(object response)
    {

        string jsonpCallback = Request.Query["callback"].ToString(),
            json = JsonConvert.SerializeObject(response);
        if (String.IsNullOrWhiteSpace(jsonpCallback))
        {
            Response.Headers.Add("Content-Type", "text/plain");
           await Response.WriteAsync(json);
        }
        else 
        {
            Response.Headers.Add("Content-Type", "application/javascript");
           await  Response.WriteAsync(String.Format("{0}({1});", jsonpCallback, json));
        }
    }

    public HttpRequest Request { get; private set; }
    public HttpResponse Response { get; private set; }
    public HttpContext Context { get; private set; }
}